/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.model.css;

public class Filter extends Style {
    String alphaFinishX;
    String alphaFinishY;
    String alphaStartX;
    String alphaStartY;
    String alphaStyle;
    String blendDuration;
    String blendTrans;
    String blurDirection;
    String blurStrength;
    String chromaColor;
    String finishOpacity;
    String glowColor;
    String glowIntensity;
    String maskColor;
    String opacity;
    String revealDuration;
    String revealTrans;
    String shadowColor;
    String shadowDirection;
    String transition;
    boolean blur;
    boolean blurOrginal;
    boolean chroma;
    boolean flipH;
    boolean flipV;
    boolean glow;
    boolean gray;
    boolean invert = false;
    boolean shadow;
    boolean xRay;

    public void setAlphaFinishX(String alphaFinishX) {
        this.alphaFinishX = alphaFinishX;
    }

    public String getAlphaFinishX() {
        return alphaFinishX;
    }

    public void setAlphaFinishY(String alphaFinishY) {
        this.alphaFinishY = alphaFinishY;
    }

    public String getAlphaFinishY() {
        return alphaFinishY;
    }

    public void setAlphaStartX(String alphaStartX) {
        this.alphaStartX = alphaStartX;
    }

    public String getAlphaStartX() {
        return alphaStartX;
    }

    public void setAlphaStartY(String alphaStartY) {
        this.alphaStartY = alphaStartY;
    }

    public String getAlphaStartY() {
        return alphaStartY;
    }

    public void setAlphaStyle(String alphaStyle) {
        this.alphaStyle = alphaStyle;
    }

    public String getAlphaStyle() {
        return alphaStyle;
    }

    public void setBlendDuration(String blendDuration) {
        this.blendDuration = blendDuration;
    }

    public String getBlendDuration() {
        return blendDuration;
    }

    public void setBlendTrans(String blendTrans) {
        this.blendTrans = blendTrans;
    }

    public String getBlendTrans() {
        return blendTrans;
    }

    public void setBlur(boolean blur) {
        this.blur = blur;
    }

    public boolean isBlur() {
        return blur;
    }

    public void setBlurDirection(String blurDirection) {
        this.blurDirection = blurDirection;
    }

    public String getBlurDirection() {
        return blurDirection;
    }

    public void setBlurOrginal(boolean blurOrginal) {
        this.blurOrginal = blurOrginal;
    }

    public boolean getBlurOrginal() {
        return blurOrginal;
    }

    public void setBlurStrength(String blurStrength) {
        this.blurStrength = blurStrength;
    }

    public String getBlurStrength() {
        return blurStrength;
    }

    public void setChroma(boolean chroma) {
        this.chroma = chroma;
    }

    public boolean isChroma() {
        return chroma;
    }

    public void setChromaColor(String chromaColor) {
        this.chromaColor = chromaColor;
    }

    public String getChromaColor() {
        return chromaColor;
    }

    public void setFinishOpacity(String finishOpacity) {
        this.finishOpacity = finishOpacity;
    }

    public String getFinishOpacity() {
        return finishOpacity;
    }

    public void setFlipH(boolean flipH) {
        this.flipH = flipH;
    }

    public boolean isFlipH() {
        return flipH;
    }

    public void setFlipV(boolean flipV) {
        this.flipV = flipV;
    }

    public boolean isFlipV() {
        return flipV;
    }

    public void setGlow(boolean glow) {
        this.glow = glow;
    }

    public boolean isGlow() {
        return glow;
    }

    public void setGlowColor(String glowColor) {
        this.glowColor = glowColor;
    }

    public String getGlowColor() {
        return glowColor;
    }

    public void setGlowIntensity(String glowIntensity) {
        this.glowIntensity = glowIntensity;
    }

    public String getGlowIntensity() {
        return glowIntensity;
    }

    public void setGray(boolean grayScale) {
        this.gray = grayScale;
    }

    public boolean isGray() {
        return gray;
    }

    public void setInvert(boolean invert) {
        this.invert = invert;
    }

    public boolean isInvert() {
        return invert;
    }

    public void setMaskColor(String maskColor) {
        this.maskColor = maskColor;
    }

    public String getMaskColor() {
        return maskColor;
    }

    public void setOpacity(String opacity) {
        this.opacity = opacity;
    }

    public String getOpacity() {
        return opacity;
    }

    public void setRevealDuration(String revealDuration) {
        this.revealDuration = revealDuration;
    }

    public String getRevealDuration() {
        return revealDuration;
    }

    public void setRevealTrans(String revealTrans) {
        this.revealTrans = revealTrans;
    }

    public String getRevealTrans() {
        return revealTrans;
    }

    public void setShadow(boolean shadow) {
        this.shadow = shadow;
    }

    public boolean isShadow() {
        return shadow;
    }

    public void setShadowColor(String shadowColor) {
        this.shadowColor = shadowColor;
    }

    public String getShadowColor() {
        return shadowColor;
    }

    public void setShadowDirection(String shadowDirection) {
        this.shadowDirection = shadowDirection;
    }

    public String getShadowDirection() {
        return shadowDirection;
    }

    public void setStyle(String pValue) {
        super.setStyle(pValue);
    }

    public String getStyle() {
        StringBuffer buffer = new StringBuffer("filter:");

        if (!isEmptyString(blendTrans)) {
            buffer.append("blendTrans(duration=" + blendTrans + ") ");
        }

        if (!isEmptyString(revealDuration)) {
            buffer.append("revealTrans(duration=" + revealDuration + ",transition=" + transition + ") ");
        }

        if (!isEmptyString(maskColor)) {
            buffer.append("mask(color=" + maskColor + ") ");
        }

        if (chroma) {
            buffer.append("chroma(color=" + chromaColor + ") ");
        }

        if (shadow) {
            buffer.append("shadow(color=" + shadowColor + ",direction=" + shadowDirection + ") ");
        }

        if (glow) {
            buffer.append("glow(color=" + glowColor + ",intensity=" + glowIntensity + ") ");
        }

        if (blur) {
            buffer.append("blur(add=" + (blurOrginal ? "1" : "0") + ",direction=" + blurDirection + ",strength=" +
                blurStrength + ") ");
        }

        if (!isEmptyString(opacity)) {
            buffer.append("alpha(opacity=" + opacity);

            if (!isEmptyString(finishOpacity)) {
                buffer.append(",finishOpacity=" + finishOpacity);
            }

            if (!isEmptyString(alphaStyle)) {
                buffer.append(",style=" + alphaStyle);
            }

            if (!isEmptyString(alphaStartX)) {
                buffer.append(",startX=" + alphaStartX);
            }

            if (!isEmptyString(alphaFinishX)) {
                buffer.append(",finishX=" + alphaFinishX);
            }

            if (!isEmptyString(alphaStartY)) {
                buffer.append(",startY=" + alphaStartY);
            }

            if (!isEmptyString(alphaFinishY)) {
                buffer.append(",finishY=" + alphaFinishY);
            }

            buffer.append(") ");
        }

        if (gray) {
            buffer.append("gray ");
        }

        if (invert) {
            buffer.append("invert ");
        }

        if (xRay) {
            buffer.append("xRay ");
        }

        if (flipV) {
            buffer.append("flipV ");
        }

        if (flipH) {
            buffer.append("flipH ");
        }

        return buffer.append(";").toString();
    }

    public void setStyleValue(String pValue) {
        flipH = pValue.indexOf("flipH") != -1;
        flipV = pValue.indexOf("flipV") != -1;
        invert = pValue.indexOf("invert") != -1;
        xRay = pValue.indexOf("xRay") != -1;
        gray = pValue.indexOf("gray") != -1;

        int from = pValue.indexOf("blur");
        int to = pValue.indexOf(")", from) + 1;

        if (from != -1) {
            blur = true;

            String blurString = pValue.substring(from, to);

            if (blurString.indexOf("add=") != -1) {
                from = blurString.indexOf("add=") + 4;
                to = blurString.indexOf(",", from);

                if (to == -1) {
                    to = blurString.indexOf(")", from);
                }

                blurOrginal = "1".equals(blurString.substring(from, to));
            }

            if (blurString.indexOf("direction=") != -1) {
                from = blurString.indexOf("direction=") + 10;
                to = blurString.indexOf(",", from);

                if (to == -1) {
                    to = blurString.indexOf(")", from);
                }

                blurDirection = blurString.substring(from, to);
            }

            if (blurString.indexOf("strength=") != -1) {
                from = blurString.indexOf("strength=") + 9;
                to = blurString.indexOf(",", from);

                if (to == -1) {
                    to = blurString.indexOf(")", from);
                }

                blurStrength = blurString.substring(from, to);
            }
        }

        from = pValue.indexOf("glow");
        to = pValue.indexOf(")", from) + 1;

        if (from != -1) {
            glow = true;

            String glowString = pValue.substring(from, to);

            if (glowString.indexOf("color=") != -1) {
                from = glowString.indexOf("color=") + 6;
                to = glowString.indexOf(",", from);

                if (to == -1) {
                    to = glowString.indexOf(")", from);
                }

                glowColor = glowString.substring(from, to);
            }

            if (glowString.indexOf("intensity=") != -1) {
                from = glowString.indexOf("intensity=") + 10;
                to = glowString.indexOf(",", from);

                if (to == -1) {
                    to = glowString.indexOf(")", from);
                }

                glowIntensity = glowString.substring(from, to);
            }
        }

        from = pValue.indexOf("shadow");
        to = pValue.indexOf(")", from) + 1;

        if (from != -1) {
            shadow = true;

            String shadowString = pValue.substring(from, to);

            if (shadowString.indexOf("color=") != -1) {
                from = shadowString.indexOf("color=") + 6;
                to = shadowString.indexOf(",", from);

                if (to == -1) {
                    to = shadowString.indexOf(")", from);
                }

                shadowColor = shadowString.substring(from, to);
            }

            if (shadowColor.indexOf("direction=") != -1) {
                from = shadowColor.indexOf("direction=") + 10;
                to = shadowColor.indexOf(",", from);

                if (to == -1) {
                    to = shadowColor.indexOf(")", from);
                }

                shadowDirection = shadowColor.substring(from, to);
            }
        }

        from = pValue.indexOf("blendTrans");
        to = pValue.indexOf(")", from) + 1;

        if (from != -1) {
            String shadowString = pValue.substring(from, to);

            if (shadowString.indexOf("duration=") != -1) {
                from = shadowString.indexOf("duration=") + 9;
                to = shadowString.indexOf(",", from);

                if (to == -1) {
                    to = shadowString.indexOf(")", from);
                }

                blendTrans = shadowString.substring(from, to);
            }
        }

        from = pValue.indexOf("revealTrans");
        to = pValue.indexOf(")", from) + 1;

        if (from != -1) {
            String shadowString = pValue.substring(from, to);

            if (shadowString.indexOf("duration=") != -1) {
                from = shadowString.indexOf("duration=") + 9;
                to = shadowString.indexOf(",", from);

                if (to == -1) {
                    to = shadowString.indexOf(")", from);
                }

                revealDuration = shadowString.substring(from, to);
            }

            if (shadowString.indexOf("transition=") != -1) {
                from = shadowString.indexOf("transition=") + 11;
                to = shadowString.indexOf(",", from);

                if (to == -1) {
                    to = shadowString.indexOf(")", from);
                }

                transition = shadowString.substring(from, to);
            }
        }

        from = pValue.indexOf("mask");
        to = pValue.indexOf(")", from) + 1;

        if (from != -1) {
            String shadowString = pValue.substring(from, to);

            if (shadowString.indexOf("color=") != -1) {
                from = shadowString.indexOf("color=") + 6;
                to = shadowString.indexOf(",", from);

                if (to == -1) {
                    to = shadowString.indexOf(")", from);
                }

                maskColor = shadowString.substring(from, to);
            }
        }

        from = pValue.indexOf("chroma");
        to = pValue.indexOf(")", from) + 1;

        if (from != -1) {
            chroma = true;

            String shadowString = pValue.substring(from, to);

            if (shadowString.indexOf("color=") != -1) {
                from = shadowString.indexOf("color=") + 6;
                to = shadowString.indexOf(",", from);

                if (to == -1) {
                    to = shadowString.indexOf(")", from);
                }

                chromaColor = shadowString.substring(from, to);
            }
        }

        from = pValue.indexOf("alpha");
        to = pValue.indexOf(")", from) + 1;

        if (from != -1) {
            String alpha = pValue.substring(from, to);

            if (alpha.indexOf("opacity=") != -1) {
                from = alpha.indexOf("opacity=") + 8;
                to = alpha.indexOf(",", from);

                if (to == -1) {
                    to = alpha.indexOf(")", from);
                }

                opacity = alpha.substring(from, to);
            }

            if (alpha.indexOf("finishOpacity=") != -1) {
                from = alpha.indexOf("finishOpacity=") + 14;
                to = alpha.indexOf(",", from);

                if (to == -1) {
                    to = alpha.indexOf(")", from);
                }

                if (from != -1) {
                    finishOpacity = alpha.substring(from, to);
                }
            }

            if (alpha.indexOf("startX=") != -1) {
                from = alpha.indexOf("startX=") + 7;
                to = alpha.indexOf(",", from);

                if (to == -1) {
                    to = alpha.indexOf(")", from);
                }

                if (from != -1) {
                    alphaStartX = alpha.substring(from, to);
                }
            }

            if (alpha.indexOf("finishX=") != -1) {
                from = alpha.indexOf("finishX=") + 8;
                to = alpha.indexOf(",", from);

                if (to == -1) {
                    to = alpha.indexOf(")", from);
                }

                if (from != -1) {
                    alphaFinishX = alpha.substring(from, to);
                }
            }

            if (alpha.indexOf("startY=") != -1) {
                from = alpha.indexOf("startY=") + 7;
                to = alpha.indexOf(",", from);

                if (to == -1) {
                    to = alpha.indexOf(")", from);
                }

                if (from != -1) {
                    alphaStartY = alpha.substring(from, to);
                }
            }

            if (alpha.indexOf("finishY=") != -1) {
                from = alpha.indexOf("finishY=") + 8;
                to = pValue.indexOf(",", from);

                if (to == -1) {
                    to = alpha.indexOf(")", from);
                }

                if (from != -1) {
                    alphaFinishY = alpha.substring(from, to);
                }
            }

            if (alpha.indexOf("style=") != -1) {
                from = alpha.indexOf("style=") + 6;
                to = alpha.indexOf(",", from);

                if (to == -1) {
                    to = alpha.indexOf(")", from);
                }

                if (from != -1) {
                    alphaStyle = alpha.substring(from, to);
                }
            }
        }
    }

    public void setTransition(String transitionShape) {
        this.transition = transitionShape;
    }

    public String getTransition() {
        return transition;
    }

    public void setXRay(boolean xRay) {
        this.xRay = xRay;
    }

    public boolean isXRay() {
        return xRay;
    }

    private boolean isEmptyString(String testString) {
        return (testString == null) || (testString.length() == 0);
    }
}
